#region Usings
using System.Collections.Generic;
using System.Linq;

using Castle.Core;
using Castle.MicroKernel.Facilities;
#endregion

namespace BK.CastleUtils
{
    internal class AttributeFacility : AbstractFacility
    {
        #region Instance Methods
        protected override void Init()
        {
            Kernel.ComponentModelCreated += KernelComponentModelCreated;
        }
        #endregion

        #region Event Handling
        private void KernelComponentModelCreated(ComponentModel model)
        {
            var attributes = model.Implementation.GetCustomAttributes(typeof(LifestyleAttribute), false);
            if (attributes.Length > 0)
            {
                var attr = attributes[0] as LifestyleAttribute;
                if (null != attr)
                {
                    switch (attr.Lifestyle)
                    {
                        case LifestyleType.PerWebRequest:
                            model.CustomLifestyle = typeof(PerWebRequestLifestyleManager);
                            model.LifestyleType = LifestyleType.Custom;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        #endregion
    }
}